﻿/**
  KCAKCAK   CAKCAKC   AK      CA
 AK        KC     CA  AK    AK
CA         KC     CA  AK  KC
CA         KCAKCAKCA  AKCA
CA         KC     CA  AK  KC
 AK        KC     CA  AK    AK
  KCAKCAK  KC     CA  AK      CA

_________________________________________________________________
 China ataca Kamchatka - copyright 2007(c)Ruben Sainz de la Maza
 
	@class: 	Chat
	@vertion: 	0.1
	@date:		25/02/08
				
	@language: 	ActionScript 3.0
	@author:	Ruben Sainz de la Maza
			
**/

try { var dummy = Chat; } catch(e)
{

	/**
	 * constructor
	 */
	function Chat()
	{
		trace("#Chat# constructor");
	}


	/**
	 * enviaMensaje
	 */
	Chat.prototype.enviaMensaje = function(p_null, p_strNick, p_strPriv, p_strMsg)
	{
		var strNick = p_strNick;
		var strPriv = p_strPriv;
		var strMsg 	= this.hiliteURLs(p_strMsg);
		strMsg		= "<b>" + strNick + ":</b>&nbsp;" + strMsg;
		
	//	trace("strPriv: "+strPriv);
		if(strPriv == strNick)return;
		
		/*
		* Enviar mensaje privado
		*/
		if((strPriv != null)&&(strPriv != undefined)&&(strPriv != "all")){			
			if(application.objUsers[strPriv] != undefined){
				var strP = "<font color='#0000FF'>" + strMsg + "</font>";
				application.objUsers[strPriv].call("Chat/recibeMensaje", false, strP);
				application.objUsers[strNick].call("Chat/recibeMensaje", false, strP);
			//	trace("Mensaje privado: "+strP);
			}
			return;
		}
	

		/*
		* Envia a todos en esta sala
		*/
		var i;
		var l 	= application.objUsers.length;
		var intRoom = application.objUsers[strNick].intRoom;
		for(i in application.objUsers){
			if(application.objUsers[i].intRoom == intRoom){
				application.objUsers[i].call("Chat/recibeMensaje", false, strMsg);
			//	trace(strMsg);
			}
		}	

	};


//  UTILS

	/**
	 * hiliteURLs
	 * 
	 * @usage   
	 * @param   p_strMsg 
	 * @return  
	 */	
	Chat.prototype.hiliteURLs = function(p_strMsg)
	{
		var strMsgC 	= p_strMsg
		var intMsgL		= strMsgC.length;
		var strEscaped 	= "";
		var intLtPos 	= strMsgC.indexOf("<");
		
		while(intLtPos != -1){
			strEscaped 	= strMsgC.substring(0, intLtPos)+ "&lt;" + strMsgC.substring(intLtPos + 1, intMsgL);
			strMsgC 		= strEscaped;
			intLtPos 	= strMsgC.indexOf("<");
		}

		var strEscaped 	= "";
		var intLtPos 	= strMsgC.indexOf(">");
		
		while(intLtPos != -1){
			strEscaped 	= strMsgC.substring(0, intLtPos)+ "&gt;" + strMsgC.substring(intLtPos + 1, intMsgL);
			strMsgC 		= strEscaped;
			intLtPos 	= strMsgC.indexOf(">");
		}

		var url_begin = strMsgC.indexOf("http:");		
		if( url_begin == -1 )url_begin = strMsgC.indexOf("www.");
		if( url_begin == -1 )return strMsgC;

		var hilited = strMsgC.substring(0, url_begin);
		var url_end = strMsgC.indexOf( " ", url_begin );

		var urlstr = "";
		if( url_end == -1 )urlstr = strMsgC.substring(url_begin);
		else				 urlstr = strMsgC.substring(url_begin, url_end);

		var urlref = urlstr;
		if( urlstr.indexOf("www.")== 0 )urlref = "http://" + urlstr;

		var trailer = "";
		if( url_end != -1 )trailer = this.hiliteURLs( strMsgC.substring(url_end));

		hilited += "<font color=\"#0000FF\"><u><a href=\"" + urlref + "\" target=\"_blank\">" + urlstr + "</a></u></font>" + trailer;

		return hilited;
	}
	

}